package com.hongdee.crm.insurance.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.insurance.entity.TCrmCarInsurance;
import com.hongdee.crm.insurance.service.ITCrmCarInsuranceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;


import java.util.List;
import java.util.Map;

/**
 * 名  称:InsuranceController
 * 类  型: class
 * 描  述:
 * 路  径: com.hongdee.crm.insurance.controller;
 * 作  者: 术洪超
 * 日  期:2017-02-15 14:19
 */

@RestController
@RequestMapping("/crm/insurance")
public class TCrmCarInsuranceController {

    @Autowired
    private ITCrmCarInsuranceService iTCrmCarInsuranceService;
    /**
     * @param  uid 登录用户id
     * @param  token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   续保明细
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = this.iTCrmCarInsuranceService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @param data  Map形式的参数
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */

    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "") String start,
                    @RequestParam(name = "limit", defaultValue = "") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = iTCrmCarInsuranceService.page(data, start, limit,uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;

    }


    /**
     * @param uid         登录用户id
     * @param token       登录用户 token
     * @param searchValue 查询条件
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/pageOwner")
    @ResponseBody
    public Map pageOwner(@RequestParam(name = "uid", defaultValue = "") String uid,
                         @RequestParam(name = "token", defaultValue = "") String token,
                         @RequestParam(name = "start", defaultValue = "0") String start,
                         @RequestParam(name = "limit", defaultValue = "50") String limit,
                         @RequestParam(name = "searchValue", defaultValue = "") String searchValue) throws Exception {
        Map map = iTCrmCarInsuranceService.pageOwner(uid, start, limit, searchValue);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param data  TCrmCarInsurance实体
     * @return Map
     * @throws Exception
     * @desc
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarInsurance data) throws Exception {
        // System.out.println(uid + "----------");

        //判断必填项
        EntityUtils.valid(data);
        String oId = TokenUtil.getCompanyId(uid);
        data.setCompanyId(oId);
        data.setCreateUser(uid);
        data.setUpdateUser(uid);
        data.setCompulsoryStartDate(data.getContractstartinsurance());
        data.setCompulsoryEndDate(data.getContractstopinsurance());
        TCrmCarInsurance ins = iTCrmCarInsuranceService.save(data);
        ResultEntity res = new ResultEntity();
        res.setData(ins);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   分页查询导入
     * @author 徐钦勇
     */
    @RequestMapping("/impPage")
    @ResponseBody
    public Map impPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        Map map = this.iTCrmCarInsuranceService.impPage(start, limit,oid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   导入确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirm")
    @ResponseBody
    public ResultEntity impConfirm(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        if(org.springframework.util.StringUtils.isEmpty(id)){
            throw new Exception("请选择记录！");
        }
        this.iTCrmCarInsuranceService.impConfirm(id, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("确认成功！");
        return res;
    }
    /**
     * @param uid 登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc   导入删除
     * @author 徐钦勇
     */
    @RequestMapping("/impDel")
    @ResponseBody
    public ResultEntity impDel(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        this.iTCrmCarInsuranceService.clearById(id);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }
    /**
     * @param data 批量数据
     * @return Map
     * @throws Exception
     * @desc   续保批量确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirms")
    @ResponseBody
    public ResultEntity impConfirms(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody List<Map> data) throws Exception {
        iTCrmCarInsuranceService.multiImpConfirm(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }
    /**
     * @return Map
     * @throws Exception
     * @desc   续保全部确认
     * @author 徐钦勇
     */
    @RequestMapping("/impConfirmAll")
    @ResponseBody
    public ResultEntity impConfirmAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        iTCrmCarInsuranceService.impConfirmAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("数据确认成功！");
        return res;
    }


    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 导入删除
     * @author 术洪超
     */
    @RequestMapping("/impDelAll")
    @ResponseBody
    public ResultEntity impDelAll(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        this.iTCrmCarInsuranceService.impDelAll(uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }



    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return Map
     * @throws Exception
     * @desc 删除多条待导入的数据
     * @author 术洪超
     */
    @RequestMapping("/impDels")
    @ResponseBody
    public ResultEntity impDels(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody List<Map<Object, String>> data) throws Exception {
        if (org.springframework.util.StringUtils.isEmpty(data)) {
            throw new Exception("请选择记录！");
        }
        this.iTCrmCarInsuranceService.impDels(data, uid);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("删除成功！");
        return res;
    }


}
